#ifndef IR_RECEIVER_H
#define IR_RECEIVER_H

#include <Arduino.h>



class IRReceiver
{
public:
    // 构造函数
    IRReceiver(uint8_t pin);

    // 初始化红外接收器
    void begin();

    // 检查是否接收到红外信号
    bool hasSignal();

    // 获取接收到的红外代码
    unsigned long getCode();

    // 执行与接收到的代码相关的操作
    void executeCommand(unsigned long code);

private:
    uint8_t pin_;
    bool initialized_;

    // 中断相关
    static volatile bool irReceived;
    static volatile unsigned long irCode;
    static volatile unsigned long lastTime;
    static volatile int bitCount;
    static volatile unsigned long data;
    static void IRAM_ATTR irInterrupt();

    // NEC协议解码
    static bool decodeNEC(unsigned long pulseWidth);
    static bool isValidPulse(unsigned long duration, unsigned long expected, unsigned long tolerance = 200);
};

// 全局红外接收器对象声明
extern IRReceiver g_irReceiver;

#endif // IR_RECEIVER_H